package org.uim.cricket.analyzer.gui;

import java.util.*;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.DefaultListModel;
import javax.swing.UIManager;

import org.uim.cricket.entities.batting.BattingInning;

/**
 *
 * @author Isuru Madushanka
 */
public class SelectCorrectPlayer extends javax.swing.JDialog {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<BattingInning> bis = null;
	private String playerName = null;
	
	private int selIndex = 0;
	
    public SelectCorrectPlayer(java.awt.Frame parent, boolean modal, List<BattingInning> bis, String toFindPlayer) {
        super(parent, modal);
        
        this.bis = bis;
        this.playerName = toFindPlayer;
        
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        initComponents();

        this.setCenterScreen();
        this.fillList();
    }

    private void setCenterScreen() {
        Dimension window = this.getSize();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        int newX = (int) (screen.getWidth() - window.getWidth()) / 2;
        int newY = (int) (screen.getHeight() - window.getHeight()) / 2;

        this.setLocation(newX, newY);
    }

    public int getSelIndex() {
    	return this.selIndex;
    }
    
    private void fillList() {
    	this.lblOutPlayer.setText(playerName);
    	
        DefaultListModel model = new DefaultListModel();
        if (this.bis != null) {
        	for (int i=0; i<bis.size(); i++) {
        		BattingInning bi = bis.get(i);
        		model.addElement(bi.getBatsman().getTitleName() + "[" + bi.getBatsman().getId() + "]");
        	}
        }
        this.lstPlayers.setModel(model);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lblOutPlayer = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstPlayers = new javax.swing.JList();
        cmdOK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select Correct Player");
        setResizable(false);

        jLabel1.setText("Player:");

        lblOutPlayer.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblOutPlayer.setText("outplayer");

        jLabel2.setText("Correct Player:");

        jScrollPane1.setViewportView(lstPlayers);

        cmdOK.setText("OK");
        cmdOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(cmdOK, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblOutPlayer)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblOutPlayer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cmdOK)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void cmdOKActionPerformed(java.awt.event.ActionEvent evt) {
        this.selIndex = this.lstPlayers.getSelectedIndex();
 
        this.setVisible(false);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        
    }

    // Variables declaration - do not modify
    private javax.swing.JButton cmdOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblOutPlayer;
    private javax.swing.JList lstPlayers;
    // End of variables declaration

}
